<?php

#################################################################
#							   HTML
#################################################################

/**
 * Messages component registed as 'element/messages'
 * 
 * $vars['messages']				Array of messages to print. Each row is espected to be an array with 2 keys: (1) the string message and (2) the boolean error flag
 * $vars['duration']				Duration of messages
 */	

if($_CONTEXT=='HTML'){
	
	$_CSS_STACK->push(blg_get_site_url().'component/messages.css', TRUE);
	$_JS_STACK->push(blg_get_site_url().'component/messages.js', TRUE);	

	if(empty($vars['duration']))
		$vars['duration']= blg_get_config('messages_duration');
	
	$messages= '';
	if(is_array($vars['messages'])){
		foreach($vars['messages'] as $i => $message){
			list($msg, $error)= $message;
			$uniqid= uniqid().'_'.random_string(5,6);		
			$messages.= '<div id="'.$uniqid.'" rel="blg_message_div" duration="'.$vars['duration'].'" class="blg_message_'.($error?'error':'normal').'">'.$msg.'</div>';		
		}	
	}
	
echo <<<HTML
<div class="blg_messages_container" id="id_blg_messages_container">$messages</div>
HTML;

}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){ ?>

/*********** MESSAGES ***********/

function blg_msg_show(id){
	$('#'+id).fadeIn(300);		
	$('#'+id).mouseup(function(){
		blg_msg_remove(id,1);	
	});
	duration= parseInt($('#'+id).attr('duration'), 10);
	setTimeout("blg_msg_remove('"+id+"',300)", duration);
}

function blg_msg_remove(id, speed){
	$('#'+id).fadeOut(speed, function(){
		$(this).remove();
	});
}

function blg_msg_add(msg, error, duration){
	uniqid= blg_uniqid_generator();
	if(error){ color= 'error'; } else { color= 'normal'; }	
		
	html= '<div id="'+uniqid+'" rel="blg_message_div" duration="'+duration+'" class="blg_message_'+color+'">'+msg+'</div>';
	
	$('#id_blg_messages_container').append(html).ready( function() {
		blg_msg_show(uniqid);
	});
}

function blg_message(msg){
	blg_msg_add(msg, false, <?php echo blg_get_config('messages_duration');?>); 	
}

function blg_error(msg){
	blg_msg_add(msg, true, <?php echo blg_get_config('messages_duration');?>); 	
}

/*********** DOCUMENT READY ***********/

$(document).ready(function(){
	
	/************* MESSAGES BEHAVIOUR *************/
	$('div[rel=blg_message_div]').each(function(index, element) {
		blg_msg_show($(this).attr('id'));
	});

});

<?php }
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>

/*********************** MESSAGES ***********************/

div.blg_messages_container{
	position: fixed;
	top: 15px;
	right: 20px;
	z-index: 20;
	text-align: right;
}

div.blg_message_normal, div.blg_message_error{
	margin: 0 0 8px 0;
	padding: 2px 10px;
	font-weight: bold;
	border-radius: 6px;
	height: 15px;
	line-height: 16px;
	font-family: "Arial";
	font-size: 11px;
	display: table;
	clear: both;
	float: right;
	text-align: left;
	display: none;
	cursor: pointer;
	
    box-shadow: 3px 3px 5px #333333;
}

div.blg_message_normal{
	background-color: <?php echo blg_color('blue', 'msg')?>;
	color: <?php echo blg_color('white', 'white')?>;
}

div.blg_message_error{
	background-color: <?php echo blg_color('red', 'msg')?>;
	color: <?php echo blg_color('black', 'black')?>;
}

<?php
}